package com.dries.portal.config;

import com.dries.common.constant.Constant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: Rabbitmq TopicExchange配置
 * @Author ZSY
 * @createTime 2020/8/3 18:03
 */
@Configuration
public class RabbitTopicConfig {

    /**
     * 创建TopicExchange
     * @return
     */
    @Bean
    TopicExchange couponTopicExchange() {
        return new TopicExchange(Constant.TOPIC_COUPON, true, false);
    }
    @Bean
    TopicExchange cashDrawsTopicExchange() { return new TopicExchange(Constant.TOPIC_CASH_DRAWS, true, false); }

    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange("convener_exchange", "x-delayed-message",true, false,args);
    }

    /**
     * 创建一个消息队列
     * @return
     */
    @Bean
    Queue coupon() {
        return new Queue("coupon");
    }
    @Bean
    Queue cashDraws() {
        return new Queue("cashDraws");
    }

    @Bean
    Queue convener() {
        return new Queue("convener", true);
    }

    /**
     * 将Queue 队列绑定到TopicExchange 中
     * 消息的routingKey 凡是以"coupon"开头，
     * 都将被路由到名称为"coupon"的Queue队列中
     * @return
     */
    @Bean
    Binding couponBinding() {
        return BindingBuilder.bind(coupon()).to(couponTopicExchange())
                .with("coupon.#");
    }
    @Bean
    Binding cashDrawsBinding() {
        return BindingBuilder.bind(cashDraws()).to(cashDrawsTopicExchange())
                .with("cashDraws.#");
    }

    @Bean
    Binding convenerBinding() {
        return BindingBuilder.bind(convener()).to(delayExchange())
                .with("convener").noargs();
    }

}
